package org.javarosa.activity.mesh4j.extensions;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import org.javarosa.core.Context;
import org.javarosa.core.api.Constants;
import org.javarosa.core.api.IActivity;
import org.javarosa.core.api.ICommand;
import org.javarosa.core.api.IShell;
import org.javarosa.core.services.UnavailableServiceException;
import org.javarosa.core.services.mesh4j.extensions.SyncEngineService;
import org.javarosa.view.MessageScreen;
import org.mesh4j.sync.validations.MeshException;

public class DeleteSyncInfoActivity implements IActivity, CommandListener{
	
	// MODEL VARIABLES
	private Context context;
	private IShell shell;
	private MessageScreen messageScreen;
	private boolean deleteXforms = false;
	private Vector<Command> commands;
	
	// BUSINESS METHODS
	
	public DeleteSyncInfoActivity(IShell mainShell) {
		this.shell = mainShell;
		this.commands = new Vector<Command>(1);
		this.commands.addElement(new Command("Ok", Command.OK, 1));
	}

	public void contextChanged(Context globalContext) {
		context.mergeInContext(globalContext);
	}

	public void destroy() {
		// Nothing for this Module
	}

	public void halt() {
		// Nothing for this Module
	}

	public void resume(Context globalContext) {	
		startDelete();
	}

	public void start(Context context) {
		this.context = context;
		startDelete();
	}

	private void startDelete() {
		this.messageScreen = new MessageScreen("Delete Sync Info.", this.commands, this);
		shell.setDisplay(this, this.messageScreen);
		new WorkerThread().start();
	}

	public void commandAction(Command c, Displayable d) {
		if (d == this.messageScreen) {
			this.shell.returnFromActivity(this, Constants.ACTIVITY_COMPLETE, null);
		}
	}
		
	private class WorkerThread implements Runnable {		
		public void start(){
			new Thread(this).start();
		}		
		public void run() {
			deleteData();
		}
	}
	
	private void deleteData() {
		try{
			this.messageScreen.setMessage("Removing sync info...");
			SyncEngineService.deleteInfo(this.deleteXforms);
			this.messageScreen.setMessage("Successfull deleted !!!");
		} catch (MeshException e) {
			e.printStackTrace();
			this.messageScreen.setMessage("Fail delete info !!!");
		} catch (UnavailableServiceException e) {
			e.printStackTrace();
			this.messageScreen.setMessage("Fail delete info !!!");
		}
	}
	
	public void setDeleteXFroms(){
		this.deleteXforms = true;
	}

	public Context getActivityContext() {
		return context;
	}

	public void setShell(IShell shell) {
		this.shell = shell;
		
	}
	/* (non-Javadoc)
	 * @see org.javarosa.core.api.IActivity#annotateCommand(org.javarosa.core.api.ICommand)
	 */
	public void annotateCommand(ICommand command) {
		throw new RuntimeException("The Activity Class " + this.getClass().getName() + " Does Not Yet Implement the annotateCommand Interface Method. Please Implement It.");
	}
}